import {HEADER_ACCEPT_LANGUAGE} from "~~/server/resources/constants/constant";
import type {H3Event} from "h3";
import {consola} from "consola";

export default defineI18nLocaleDetector((event: H3Event, config: { defaultLocale: any; }) => {
    const query = tryQueryLocale(event, {lang: ''})
    if (query) {
        return query.toString()
    }
    const key = useRuntimeConfig().public.languageCookieKey;
    const cookie = tryCookieLocale(event, {lang: '', name: key})
    if (cookie) {
        return cookie.toString().toLowerCase();
    }
    const header = tryHeaderLocale(event, {lang: '', name: HEADER_ACCEPT_LANGUAGE})
    if (header) {
        return header.toString().toLowerCase();
    }

    return config.defaultLocale
})